package hit.example.home;

import hit.example.Menu.ViewMenu;
import hit.example.Object.ObjWord;
import hit.example.adapter.AdapterListView;
import hit.example.hitdict.R;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.telpoo.frame.delegate.Idelegate;
import com.telpoo.frame.ui.BaseFragmentActivity;
import com.telpoo.frame.utils.TaskDownloadFile;

public class MyHomeActivity extends SlidingFragmentActivity implements
		Idelegate, OnClickListener {
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

	private ProgressDialog mProgressDialog;
	int resource_home;
	String toastAskExit;
	static String[] tabids = { TabId.home };
	ViewMenu viewMenu;
	ImageView btnMenu, btnChuyendoi;
	View fm_top;
	TextView tvTitle;
	EditText edtSearch;
	ArrayList<ObjWord> listWord;
	AdapterListView adpListView;
	int ngonngu = 0;
	public View canvasView;
	public View targetView;
	String pathStorage = Environment.getExternalStorageDirectory()
			.getAbsolutePath() + "/Hit_Dict/db/db.zip";

	String pathStorageUnzip = Environment.getExternalStorageDirectory()
			.getAbsolutePath() + "/Hit_Dict/db/";

	public MyHomeActivity() {
		super(tabids, R.id.realTabContent, "Bấm thoát lần nữa để thoát !");
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {

		setupTracking();
		super.onCreate(savedInstanceState);
		initView();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		initAds();

	}

	private void initAds() {

	}

	private void setupTracking() {
		// setTrackingId("UA-33222928-5");

	}

	private void initView() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		getSlidingMenu().setMode(SlidingMenu.LEFT);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		setBehindContentView(viewMenu.getView());

		getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
		getSlidingMenu().setShadowDrawable(R.drawable.shadow);
		getSlidingMenu().setBehindOffsetRes(R.dimen.slidingmenu_offset);
		getSlidingMenu().setFadeDegree(0.35f);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		btnMenu = (ImageView) findViewById(R.id.btnMenu);
		btnChuyendoi = (ImageView) findViewById(R.id.btnChuyendoi);
		btnChuyendoi.setOnClickListener(this);
		btnMenu.setOnClickListener(this);
		fm_top = findViewById(R.id.fm_top);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		edtSearch = (EditText) findViewById(R.id.edtSearch);
		targetView = findViewById(R.id.idCapture);

	}

	@Override
	public void callBack(Object value, int where) {
	}

	@Override
	public void onSuccess(int taskType, ArrayList<?> list, String msg) {
		super.onSuccess(taskType, list, msg);
		if (list == null || list.size() == 0)
			return;
	}

	@Override
	public void onFail(int taskType, String msg) {
		super.onFail(taskType, msg);
	}

	@Override
	public BaseFragmentActivity getRootFA() {
		return super.getRootFA();
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnMenu:

			toggle();

			break;
		case R.id.btnChuyendoi:
			if (ngonngu == 0) {
				ngonngu = 1;
				tvTitle.setText("Vietnamses - English");
			} else {
				ngonngu = 0;
				tvTitle.setText("English - Vietnamses");
			}
		default:
			break;
		}

	}

	public void toggle() {
		getSlidingMenu().toggle(true);
	}

	private void startDownload() {
		String url = "http://app.vietbao.vn/service/apptudien/data/anh_viet.zip";
		new DownloadFileAsync().execute(url);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DOWNLOAD_PROGRESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setMessage("Downloading file..");
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgressDialog.setCancelable(false);
			mProgressDialog.show();
			return mProgressDialog;
		default:
			return null;
		}
	}

	class DownloadFileAsync extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showDialog(DIALOG_DOWNLOAD_PROGRESS);
		}

		@Override
		protected String doInBackground(String... aurl) {
			int count;

			try {

				URL url = new URL(aurl[0]);
				URLConnection conexion = url.openConnection();
				conexion.connect();

				int lenghtOfFile = conexion.getContentLength();
				Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

				InputStream input = new BufferedInputStream(url.openStream());
				OutputStream output = new FileOutputStream(
						Environment.getExternalStorageState()
								+ "/Hit Dict/Db/anh_viet.zip");

				byte data[] = new byte[1024];

				long total = 0;

				while ((count = input.read(data)) != -1) {
					total += count;
					publishProgress("" + (int) ((total * 100) / lenghtOfFile));
					output.write(data, 0, count);
				}

				output.flush();
				output.close();
				input.close();
			} catch (Exception e) {
			}
			return null;

		}

		protected void onProgressUpdate(String... progress) {
			Log.d("ANDRO_ASYNC", progress[0]);
			mProgressDialog.setProgress(Integer.parseInt(progress[0]));
		}

		@Override
		protected void onPostExecute(String unused) {
			dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
		}
	}

}
